package com.fun.feifun.modules.sys.auth.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fun.feifun.base.result.Result;
import com.fun.feifun.modules.sys.auth.model.SysRole;
import com.fun.feifun.modules.sys.auth.model.param.RoleMenuParam;
import com.fun.feifun.modules.sys.auth.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* <p>
* 系统角色 接口控制器
* </p>
*
* @author wkf
* @since 2022-04-21
*/
@RestController
@Slf4j
@Api(tags = "系统-角色")
@RequestMapping("/sys/role")
@SaCheckLogin()
public class SysRoleController {


    @Resource
    private ISysRoleService sysRoleService;


    @PostMapping(value = "/save")
    @ApiOperation(value = "添加或者修改")
    public Result save(@Validated @RequestBody RoleMenuParam item) {
        if( sysRoleService.saveRoleSetMenus(item)){
            return Result.success();
        }
        return Result.err();
    }

    @PostMapping(value = "/removes")
    @ApiOperation(value = "删除")
    public  Result removes(@Validated @RequestBody List<Long> ids) {
		sysRoleService.removeByIds(ids);
        return Result.success();
    }


    @GetMapping("/findById")
    @ApiOperation(value = "根据ID查找数据")
    public Result findById(@Validated @RequestParam("id") Long id){
        SysRole item = sysRoleService.getById(id);
        if(item==null){
            return Result.err("尚未查询到此ID");
        }
        return Result.success(item);
    }


    @GetMapping("/all")
    @ApiOperation(value = "获取全部数据")
    public Result< List<SysRole>> getAll(){
        List<SysRole> all = sysRoleService.list();
        return Result.success(all);
    }


    @PostMapping("/page")
    @ApiOperation(value = "获取分页信息")
    public Result< Page<SysRole>> page(
        @RequestParam(name = "page", defaultValue = "1") int index,
        @RequestParam(name = "size", defaultValue = "20") int size){
        Page page = new Page(index,size);
		sysRoleService.page(page);
        return Result.success(page);
    }

    @PostMapping("/roleSetMenu")
    @ApiOperation(value = "角色设置菜单")
    public Result roleSetMenu(@Validated @RequestBody RoleMenuParam param){
        sysRoleService.setRoleMenus(param.getId(),param.getMenuIds());
        return Result.success();
    }

}
